<template>
  <div class="login">
    <el-form
      label-position="top"
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="login-form"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          :loading="isLogin"
          @click="onSubmit"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import { login } from "@/services/user";
import { Form } from "element-ui";

export default Vue.extend({
  name: "LoginIndex",
  data () {
    return {
      form: {
        phone: "",
        password: "",
      },
      isLogin: false,
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1\d{10}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 个以上的字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async onSubmit () {
      try {
        await (this.$refs.form as Form).validate();
        this.isLogin = true;
        const { data } = await login(this.form);
        if (data?.state !== 1) {
          return this.$message.error(data.message);
        }
        this.$store.commit('setUser', data.content);
        // this.$router.push({
        //   name: "home",
        // });
        this.$router.push(this.$route.query.redirect as string || '/');
        this.$message.success("登录成功");
      } catch (error) {
        this.$message.error("登录失败");
      } finally {
        this.isLogin = false;
      }
    },
  },
});
</script>

<style lang="scss" scoped>
.login {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    width: 300px;
    background: #fff;
    padding: 20px;
    border-radius: 5px;
  }

  .login-btn {
    width: 100%;
  }
}
</style>